package com.yourname.myapplication.activity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.RadioButton
import android.widget.RadioGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yourname.myapplication.R
import com.yourname.myapplication.ui.viewmodel.SequentialPracticeViewModel

class SequentialPracticeActivity : AppCompatActivity() {
    private lateinit var viewModel: SequentialPracticeViewModel
    private lateinit var questionTextView: TextView
    private lateinit var nextButton: Button
    private lateinit var answerRadioGroup: RadioGroup
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sequential_practice)

        viewModel = ViewModelProvider(this).get(SequentialPracticeViewModel::class.java)
        questionTextView = findViewById(R.id.recyclerView)
        nextButton = findViewById(R.id.nextButton)

        updateQuestionDisplay()

        nextButton.setOnClickListener {
            viewModel.nextQuestion()
            updateQuestionDisplay()
        }
        answerRadioGroup = findViewById(R.id.answer_radio_group)

        updateQuestionDisplay()

        nextButton.setOnClickListener {
            val selectedRadioButtonId = answerRadioGroup.checkedRadioButtonId
            if (selectedRadioButtonId != -1) {
                val selectedRadioButton = findViewById<RadioButton>(selectedRadioButtonId)
                val selectedAnswer = selectedRadioButton.text.toString()
                val currentQuestion = viewModel.getCurrentQuestion()
                currentQuestion?.let { question ->
                    if (selectedAnswer == question.answer) {
                        // 处理回答正确逻辑，比如提示正确
                    } else {
                        // 处理回答错误逻辑，比如提示错误
                    }
                }
            }
            viewModel.nextQuestion()
            updateQuestionDisplay()
            answerRadioGroup.clearCheck()
        }
        nextButton.setOnClickListener {
            val selectedRadioButtonId = answerRadioGroup.checkedRadioButtonId
            if (selectedRadioButtonId != -1) {
                val selectedRadioButton = findViewById<RadioButton>(selectedRadioButtonId)
                val selectedAnswer = selectedRadioButton.text.toString()
                val currentQuestion = viewModel.getCurrentQuestion()
                currentQuestion?.let { question ->
                    question.isAnswered = true
                    if (selectedAnswer == question.answer) {
                        question.isCorrect = true
                        // 处理回答正确逻辑，比如提示正确
                    } else {
                        question.isCorrect = false
                        // 处理回答错误逻辑，比如提示错误
                    }
                    // 这里可以添加将题目状态更新到数据库的逻辑
                }
            }
            viewModel.nextQuestion()
            updateQuestionDisplay()
            answerRadioGroup.clearCheck()
        }
    }

    private fun updateQuestionDisplay() {
        val currentQuestion = viewModel.getCurrentQuestion()
        currentQuestion?.let { question ->
            questionTextView.text = question.questionText
            answerRadioGroup.removeAllViews()
            question.options.forEach { option ->
                val radioButton = RadioButton(this)
                radioButton.text = option
                answerRadioGroup.addView(radioButton)
            }
        }
    }
}



